home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 5 / Skunkware 5.iso / src / Tools / Mesa-1.2.1 / samples / stretch.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-07-05  |  8.3 KB  |  369 lines

  1. /*
  2.  * Copyright (c) 1991, 1992, 1993 Silicon Graphics, Inc.
  3.  *
  4.  * Permission to use, copy, modify, distribute, and sell this software and
  5.  * its documentation for any purpose is hereby granted without fee, provided
  6.  * that (i) the above copyright notices and this permission notice appear in
  7.  * all copies of the software and related documentation, and (ii) the name of
  8.  * Silicon Graphics may not be used in any advertising or
  9.  * publicity relating to the software without the specific, prior written
  10.  * permission of Silicon Graphics.
  11.  *
  12.  * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF
  13.  * ANY KIND,
  14.  * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
  15.  * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
  16.  *
  17.  * IN NO EVENT SHALL SILICON GRAPHICS BE LIABLE FOR
  18.  * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
  19.  * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  20.  * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
  21.  * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
  22.  * OF THIS SOFTWARE.
  23.  */
  24.  
  25. #include <stdio.h>
  26. #include <stdlib.h>
  27. #include <string.h>
  28. #include <sys/types.h>
  29. #include <sys/stat.h>
  30. #include <malloc.h>
  31. #include <fcntl.h>
  32. #include <unistd.h>
  33. #include <math.h>
  34. #include "tk.h"
  35.  
  36.  
  37. #define STEPCOUNT 40
  38. #define FALSE 0
  39. #define TRUE 1
  40. #define MAX(a, b) (((a) > (b)) ? (a) : (b))
  41. #define MIN(a, b) (((a) < (b)) ? (a) : (b))
  42.  
  43.  
  44. enum {
  45.     OP_NOOP = 0,
  46.     OP_STRETCH,
  47.     OP_DRAWPOINT,
  48.     OP_DRAWIMAGE
  49. };
  50.  
  51.  
  52. typedef struct _cRec {
  53.     float x, y;
  54. } cRec;
  55.  
  56. typedef struct _vertexRec {
  57.     float x, y;
  58.     float dX, dY;
  59.     float tX, tY;
  60. } vertexRec;
  61.  
  62.  
  63. GLenum doubleBuffer, directRender;
  64. int imageSizeX, imageSizeY;
  65. char *fileName = 0;
  66. TK_RGBImageRec *image;
  67. cRec cList[50];
  68. vertexRec vList[5];
  69. int cCount, cIndex[2], cStep;
  70. GLenum op = OP_NOOP;
  71.  
  72.  
  73. void DrawImage(void)
  74. {
  75.  
  76.     glRasterPos2i(0, 0);
  77.     glDrawPixels(image->sizeX, image->sizeY, GL_RGB, GL_UNSIGNED_BYTE,
  78.          image->data);
  79.  
  80.     glFlush();
  81.     if (doubleBuffer) {
  82.     tkSwapBuffers();
  83.     }
  84.  
  85.     glRasterPos2i(0, 0);
  86.     glDrawPixels(image->sizeX, image->sizeY, GL_RGB, GL_UNSIGNED_BYTE,
  87.          image->data);
  88. }
  89.  
  90. void DrawPoint(void)
  91. {
  92.     int i;
  93.  
  94.     glColor3f(1.0, 0.0, 1.0);
  95.     glPointSize(3.0);
  96.     glBegin(GL_POINTS);
  97.     for (i = 0; i < cCount; i++) {
  98.         glVertex2f(cList[i].x, cList[i].y);
  99.     }
  100.     glEnd();
  101.  
  102.     glFlush();
  103.     if (doubleBuffer) {
  104.     tkSwapBuffers();
  105.     }
  106. }
  107.  
  108. void InitVList(void)
  109. {
  110.  
  111.     vList[0].x = 0.0;
  112.     vList[0].y = 0.0;
  113.     vList[0].dX = 0.0;
  114.     vList[0].dY = 0.0;
  115.     vList[0].tX = 0.0;
  116.     vList[0].tY = 0.0;
  117.  
  118.     vList[1].x = (float)imageSizeX;
  119.     vList[1].y = 0.0;
  120.     vList[1].dX = 0.0;
  121.     vList[1].dY = 0.0;
  122.     vList[1].tX = 1.0;
  123.     vList[1].tY = 0.0;
  124.  
  125.     vList[2].x = (float)imageSizeX;
  126.     vList[2].y = (float)imageSizeY;
  127.     vList[2].dX = 0.0;
  128.     vList[2].dY = 0.0;
  129.     vList[2].tX = 1.0;
  130.     vList[2].tY = 1.0;
  131.  
  132.     vList[3].x = 0.0;
  133.     vList[3].y = (float)imageSizeY;
  134.     vList[3].dX = 0.0;
  135.     vList[3].dY = 0.0;
  136.     vList[3].tX = 0.0;
  137.     vList[3].tY = 1.0;
  138.  
  139.     vList[4].x = cList[0].x;
  140.     vList[4].y = cList[0].y;
  141.     vList[4].dX = (cList[1].x - cList[0].x) / STEPCOUNT;
  142.     vList[4].dY = (cList[1].y - cList[0].y) / STEPCOUNT;
  143.     vList[4].tX = cList[0].x / (float)imageSizeX;
  144.     vList[4].tY = cList[0].y / (float)imageSizeY;
  145. }
  146.  
  147. void ScaleImage(int sizeX, int sizeY)
  148. {
  149.     GLubyte *buf;
  150.  
  151.     buf = (GLubyte *)malloc(3*sizeX*sizeY);
  152.     gluScaleImage(GL_RGB, image->sizeX, image->sizeY, GL_UNSIGNED_BYTE,
  153.                   image->data, sizeX, sizeY, GL_UNSIGNED_BYTE, buf);
  154.     free(image->data);
  155.     image->data = buf;
  156.     image->sizeX = sizeX;
  157.     image->sizeY = sizeY;
  158. }
  159.  
  160. void SetPoint(int x, int y)
  161. {
  162.  
  163.     cList[cCount].x = (float)x;
  164.     cList[cCount].y = (float)y;
  165.     cCount++;
  166. }
  167.  
  168. void Stretch(void)
  169. {
  170.  
  171.     glBegin(GL_TRIANGLES);
  172.     glTexCoord2f(vList[0].tX, vList[0].tY);
  173.     glVertex2f(vList[0].x, vList[0].y);
  174.     glTexCoord2f(vList[1].tX, vList[1].tY);
  175.     glVertex2f(vList[1].x, vList[1].y);
  176.     glTexCoord2f(vList[4].tX, vList[4].tY);
  177.     glVertex2f(vList[4].x, vList[4].y);
  178.     glEnd();
  179.  
  180.     glBegin(GL_TRIANGLES);
  181.     glTexCoord2f(vList[1].tX, vList[1].tY);
  182.     glVertex2f(vList[1].x, vList[1].y);
  183.     glTexCoord2f(vList[2].tX, vList[2].tY);
  184.     glVertex2f(vList[2].x, vList[2].y);
  185.     glTexCoord2f(vList[4].tX, vList[4].tY);
  186.     glVertex2f(vList[4].x, vList[4].y);
  187.     glEnd();
  188.  
  189.     glBegin(GL_TRIANGLES);
  190.     glTexCoord2f(vList[2].tX, vList[2].tY);
  191.     glVertex2f(vList[2].x, vList[2].y);
  192.     glTexCoord2f(vList[3].tX, vList[3].tY);
  193.     glVertex2f(vList[3].x, vList[3].y);
  194.     glTexCoord2f(vList[4].tX, vList[4].tY);
  195.     glVertex2f(vList[4].x, vList[4].y);
  196.     glEnd();
  197.  
  198.     glBegin(GL_TRIANGLES);
  199.     glTexCoord2f(vList[3].tX, vList[3].tY);
  200.     glVertex2f(vList[3].x, vList[3].y);
  201.     glTexCoord2f(vList[0].tX, vList[0].tY);
  202.     glVertex2f(vList[0].x, vList[0].y);
  203.     glTexCoord2f(vList[4].tX, vList[4].tY);
  204.     glVertex2f(vList[4].x, vList[4].y);
  205.     glEnd();
  206.  
  207.     glFlush();
  208.     if (doubleBuffer) {
  209.     tkSwapBuffers();
  210.     }
  211.  
  212.     if (++cStep < STEPCOUNT) {
  213.     vList[4].x += vList[4].dX;
  214.     vList[4].y += vList[4].dY;
  215.     } else {
  216.     cIndex[0] = cIndex[1];
  217.     cIndex[1] = cIndex[1] + 1;
  218.     if (cIndex[1] == cCount) {
  219.         cIndex[1] = 0;
  220.     }
  221.     vList[4].dX = (cList[cIndex[1]].x - cList[cIndex[0]].x) / STEPCOUNT;
  222.     vList[4].dY = (cList[cIndex[1]].y - cList[cIndex[0]].y) / STEPCOUNT;
  223.     cStep = 0;
  224.     }
  225. }
  226.  
  227. GLenum Key(int key, GLenum mask)
  228. {
  229.  
  230.     switch (key) {
  231.       case TK_ESCAPE:
  232.     free(image->data);
  233.         tkQuit();
  234.       case TK_SPACE:
  235.     if (cCount > 1) {
  236.         InitVList();
  237.         cIndex[0] = 0;
  238.         cIndex[1] = 1;
  239.         cStep = 0;
  240.         glEnable(GL_TEXTURE_2D);
  241.         op = OP_STRETCH;
  242.     }
  243.     break;
  244.       default:
  245.     return GL_FALSE;
  246.     }
  247.     return GL_TRUE;
  248. }
  249.  
  250. GLenum Mouse(int mouseX, int mouseY, GLenum button)
  251. {
  252.  
  253.     if (op == OP_STRETCH) {
  254.     glDisable(GL_TEXTURE_2D);
  255.     cCount = 0;
  256.     op = OP_DRAWIMAGE;
  257.     } else {
  258.     SetPoint(mouseX, imageSizeY-mouseY);
  259.     op = OP_DRAWPOINT;
  260.     }
  261.     return GL_TRUE;
  262. }
  263.  
  264. void Animate(void)
  265. {
  266.  
  267.     switch (op) {
  268.       case OP_STRETCH:
  269.     Stretch();
  270.     break;
  271.       case OP_DRAWPOINT:
  272.     DrawPoint();
  273.     break;
  274.       case OP_DRAWIMAGE:
  275.     DrawImage();
  276.     break;
  277.     }
  278. }
  279.  
  280. static GLenum Args(int argc, char **argv)
  281. {
  282.     GLint i;
  283.  
  284.     doubleBuffer = GL_FALSE;
  285.     directRender = GL_TRUE;
  286.  
  287.     for (i = 1; i < argc; i++) {
  288.     if (strcmp(argv[i], "-sb") == 0) {
  289.         doubleBuffer = GL_FALSE;
  290.     } else if (strcmp(argv[i], "-db") == 0) {
  291.         doubleBuffer = GL_TRUE;
  292.     } else if (strcmp(argv[i], "-dr") == 0) {
  293.         directRender = GL_TRUE;
  294.     } else if (strcmp(argv[i], "-ir") == 0) {
  295.         directRender = GL_FALSE;
  296.     } else if (strcmp(argv[i], "-f") == 0) {
  297.         if (i+1 >= argc || argv[i+1][0] == '-') {
  298.         printf("-f (No file name).\n");
  299.         return GL_FALSE;
  300.         } else {
  301.         fileName = argv[++i];
  302.         }
  303.     } else {
  304.         printf("%s (Bad option).\n", argv[i]);
  305.         return GL_FALSE;
  306.     }
  307.     }
  308.     return GL_TRUE;
  309. }
  310.  
  311. void main(int argc, char **argv)
  312. {
  313.     GLenum type;
  314.  
  315.     if (Args(argc, argv) == GL_FALSE) {
  316.     tkQuit();
  317.     }
  318.  
  319.     if (fileName == 0) {
  320.     printf("No image file.\n");
  321.     tkQuit();
  322.     }
  323.  
  324.     image = tkRGBImageLoad(fileName);
  325.  
  326.     /* changed powf and logf to pow and log -Brian */
  327.     imageSizeX = (int)pow(2.0, (float)((int)(log(image->sizeX)/log(2.0))));
  328.     imageSizeY = (int)pow(2.0, (float)((int)(log(image->sizeY)/log(2.0))));
  329.  
  330.     tkInitPosition(0, 0, imageSizeX, imageSizeY);
  331.  
  332.     type = TK_RGB;
  333.     type |= (doubleBuffer) ? TK_DOUBLE : TK_SINGLE;
  334.     type |= (directRender) ? TK_DIRECT : TK_INDIRECT;
  335.     tkInitDisplayMode(type);
  336.  
  337.     if (tkInitWindow("Stretch") == GL_FALSE) {
  338.         tkQuit();
  339.     }
  340.  
  341.     glViewport(0, 0, imageSizeX, imageSizeY);
  342.     gluOrtho2D(0, imageSizeX, 0, imageSizeY);
  343.     glClearColor(0.0, 0.0, 0.0, 0.0);
  344.  
  345.     glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
  346.     glPixelStorei(GL_PACK_ALIGNMENT, 1);
  347.  
  348.     ScaleImage(imageSizeX, imageSizeY);
  349.  
  350.     glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
  351.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
  352.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
  353.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  354.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  355.     glTexImage2D(GL_TEXTURE_2D, 0, 3, image->sizeX, image->sizeY, 0,
  356.                  GL_RGB, GL_UNSIGNED_BYTE, (unsigned char *)image->data);
  357.  
  358.     cCount = 0;
  359.     cIndex[0] = 0;
  360.     cIndex[1] = 0;
  361.     cStep = 0;
  362.     op = OP_DRAWIMAGE;
  363.  
  364.     tkKeyDownFunc(Key);
  365.     tkMouseDownFunc(Mouse);
  366.     tkIdleFunc(Animate);
  367.     tkExec();
  368. }
  369.